package kz.kstu.ds.main;

import kz.kstu.ds.generate.GenerateKeyFrame;
import kz.kstu.ds.sign.SignFrame;
import kz.kstu.ds.utils.GUIFactory;
import kz.kstu.ds.utils.GUIIcon;
import kz.kstu.ds.utils.GUIUtils;
import kz.kstu.ds.utils.StringUtil;
import kz.kstu.ds.verify.VerifyFrame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

/**
 * Date: 03.11.12
 * Time: 21:03
 *
 * @author Oleg Anastassov
 */
public class AuthCenterFrame extends JFrame {

    private static final String TITLE = "Центр аутентификации";

    private static final int HEIGHT = 230;
    private static final int WIDTH = 400;

    private JPanel rootPanel;
    private JButton generateButton;
    private JButton verifyButton;
    private JLabel imageLabel;
    private JButton closeButton;
    private JButton aboutButton;
    private JButton signButton;

    public AuthCenterFrame() {
        setTitle(TITLE);
        setSize(new Dimension(WIDTH, HEIGHT));
        setResizable(false);
        setContentPane(rootPanel);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        initIcons();
        initListeners();
    }

    private void initIcons() {
        URL imageUrl = this.getClass().getResource("/ds.jpg");
        try {
            BufferedImage image = ImageIO.read(imageUrl);
            ImageIcon imageIcon = new ImageIcon(image);
            imageLabel.setText(StringUtil.EMPTY_STRING);
            imageLabel.setIcon(imageIcon);
        } catch (IOException ignored) {
        }

        setIconImage(GUIFactory.getImage(GUIIcon.LOGO));
        generateButton.setIcon(GUIFactory.getIcon(GUIIcon.CARD));
        verifyButton.setIcon(GUIFactory.getIcon(GUIIcon.VERIFY));
        signButton.setIcon(GUIFactory.getIcon(GUIIcon.SIGN));
        aboutButton.setIcon(GUIFactory.getIcon(GUIIcon.ABOUT));
        closeButton.setIcon(GUIFactory.getIcon(GUIIcon.CLOSE));
    }

    private void initListeners() {
        generateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GenerateKeyFrame frame = new GenerateKeyFrame();
                GUIUtils.centerAndShow(frame);
            }
        });
        signButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SignFrame frame = new SignFrame();
                GUIUtils.centerAndShow(frame);
            }
        });
        verifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VerifyFrame frame = new VerifyFrame();
                GUIUtils.centerAndShow(frame);
            }
        });
        aboutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AboutFrame frame = new AboutFrame();
                GUIUtils.centerAndShow(frame);
            }
        });
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

}
